﻿using MachineVision.API.Models;
using System.Net;
using System.Text.Json;

namespace MachineVision.API.Middlewares
{
    public class ExceptionHandlingMiddleware
    {
        private readonly RequestDelegate _next;  // 用来处理上下文请求  
        private readonly ILogger<ExceptionHandlingMiddleware> _logger;
        public ExceptionHandlingMiddleware(RequestDelegate next, ILogger<ExceptionHandlingMiddleware>   logger)
        {
            _next = next;
            _logger = logger;
        }
  
        public async Task InvokeAsync(HttpContext httpContext)
        {
            try
            {
                await _next(httpContext); //要么在中间件中处理，要么被传递到下一个中间件中去
            }
            catch (Exception ex)
            {
                await HandleExceptionAsync(httpContext, ex); // 捕获异常了 在HandleExceptionAsync中    处理
            }
        }
        private async Task HandleExceptionAsync(HttpContext context, Exception exception)
        {
            context.Response.ContentType = "application/json";  // 返回json 类型
            var response = context.Response;
            var errorResponse = new ApiResult
            {
                 IsSuccess = false,
                 code = -1
            };  // 自定义的异常错误信息类型

            switch (exception)
            {
                case ApplicationException ex:
                if (ex.Message.Contains("Invalid token"))
                        {
                            response.StatusCode = (int)HttpStatusCode.Forbidden;
                            errorResponse.msg = ex.Message;
                            break;
                        }
                    response.StatusCode = (int)HttpStatusCode.BadRequest;
                    errorResponse.msg = ex.Message;
                    break;

                case KeyNotFoundException ex:
                response.StatusCode = (int)HttpStatusCode.NotFound;
                    errorResponse.msg = ex.Message;
                    break;

                default:
                response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    errorResponse.msg =exception.Message+"~"+exception.StackTrace;// "Internal Server errors. Check Logs!";
                    break;
            }
          _logger.LogError(exception.Message);

          var result = JsonSerializer.Serialize(errorResponse);
          await context.Response.WriteAsync(result);
      }

    }
}
